<!--资源管理-->
<template>
  <div class="app-container">
    <el-button @click="toAdd(0,'无父资源')" style="margin: 10px 0;float: right;" class="filter-item" type="primary"  icon="el-icon-plus">
      添加
    </el-button>
    <el-table
      :data="tableData"
      style="width: 100%;min-height: 45vh"
      row-key="resourceUrl"
      class="tree-table"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="resourceName" label="资源名称" width="180"></el-table-column>
      <el-table-column prop="resourceUrl" label="资源路径" width="180"></el-table-column>
      <el-table-column prop="code" label="权限标识"></el-table-column>
      <el-table-column prop="type" label="类型"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column label="操作" width="230" align="center">
      	<template slot-scope="scope">
      		<el-button type="primary" circle icon="el-icon-plus" @click="toAdd(scope.row.id,scope.row.resourceName)"></el-button>
          <el-button circle icon="el-icon-delete" type="danger" @click="deleteorDealer(scope.row)"></el-button>
      	</template>
      </el-table-column>
    </el-table>
    <el-dialog class="lnk-dialog"
               :close-on-click-modal="false"
               :title="dialogTitle"
               top="10vh"
               width="35%"
               :visible.sync="newDialog"
               :append-to-body="true" >
      <el-form ref="mainForm" :model="formData" :rules="rules"
               size="mini" class="editForm" label-width="80px">
        <!--表单项-->
        <el-row>
          <el-col>
            <el-form-item label="资源名称" prop="resourceName" class="car-form-item" >
              <el-input class="car-modal-col" v-model="formData.resourceName" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="所属资源" prop="parentId" class="car-form-item">
              <el-input class="car-modal-col" v-model="parentName" disabled="true" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="资源路径" prop="resourceUrl" class="car-form-item">
              <el-input class="car-modal-col" v-model="formData.resourceUrl" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="权限标识" prop="code" class="car-form-item">
              <el-input class="car-modal-col" v-model="formData.code" placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="备注" prop="remark" class="car-form-item">
              <el-input class="car-modal-col" v-model="formData.remark" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  @click="newDialog = false;$refs['mainForm'].resetFields();">取消</el-button>
        <el-button type="primary" @click="addData()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
  .table-pagination {
    margin-top: 20px;
    text-align: right;
  }

  .right-btn {
    float: right;
  }
  ::v-deep .el-table th > .cell{
    padding: 11px!important;
    font-size: 14px;
  }

  ::v-deep .el-table th{
    background-color:#fafafa;
    padding: 0!important;
  }
  ::v-deep .el-table td .cell{
    line-height: 32px;
  }

  ::v-deep .el-table__body tr:nth-child(2n) {
    background-color: #F1F4F9 !important;
  }
  .dialog-footer {
    text-align: center;
  }

  .car-modal-col {
    width: 280px;
  }
  .tree-table{
    border-color: #eee;
  }
  .el-table>.has-gutter>tr{
    background-color: #fafafa!important;
    color: #000000!important;
    font-size: 14px!important;
    .cell{
      color: #000000!important;
      font-size: 14px!important;
    }
  }
  ::v-deep .el-table th.is-leaf{
    color: #000!important;
  }
</style>

<script>

  export default {
    data() {
      return {
        rules: {
          nickName: [{required: true, message: '必输', trigger: 'blur'}]
        }, // 表单校验规则
        tableData: [],
        dialogTitle: '新增资源', // 弹框名称
        editorFlag: false,
        newDialog: false, // 添加弹框
        formData: {}, // 添加表单
        appId:"",
        parentName: "无父资源",
        listQuery: {},
        rowStyleOption: { // 表格样式
          stripe: true, // 斑马纹
          hoverHighlight: true, // hover高亮
          clickHighlight: true // 点击行高亮
        },
        checkboxOptipon: {
          // 行选择改变事件
          selectedRowChange: ({row, isSelected, selectedRowKeys}) => {
            console.log(row, isSelected, selectedRowKeys)
          },
          // 全选改变事件
          selectedAllChange: ({isSelected, selectedRowKeys}) => {
            console.log(isSelected, selectedRowKeys)
          }
        },
      }
    },
    computed: {
    },
    created() {
       this.initDatabase();
    },
    methods: {


      /**
       * 初始化数据
       * @date 2021-05-10
       */
      async initDatabase() {
        this.tableData = [];
        let data = await this.$http.get('/auth/resource/list', {});
        console.log(data);
        if (data.code === 200) {
          this.tableData = data.data;
        } else {
          this.$message({
            type: 'error',
            message: '查询失败，请刷新页面'
          });
        }
        console.log(this.tableData)
      },
      /**
       * 删除
       * @param {Object} row
       */
      async deleteorDealer(row) {

        let that = this

        let ids = row.id;
        //补个迭代方法

        this.$alert('是否确认删除此资源以及子资源', '确认删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let data = await that.$http.delete('/auth/resource/delete/' + row.id, {})
          console.log(data);
          if (data.code === 200) {
            that.initDatabase();
          } else {
            that.$message({
              type: 'info',
              message: '操作失败，请重试'
            });
          }
        })
      },

      /**
       * 编辑
       * @date 2021-05-08
       * @param row
       */
      editorDealer(row) {
        this.formData = row
        this.newDialog = true
        this.editorFlag = true // 编辑标识
        this.dialogTitle = '编辑经销商'
      },
      /**
       * 点击新增按钮
       * @date 2021-05-08
       */
      toAdd(parentId,parentName) {
        this.parentName = parentName;

        this.formData = {parentId:parentId}
        this.newDialog = true
        this.editorFlag = false  // 编辑标志
        this.dialogTitle = '新增资源'
      },
      /**
       * 新增操作
       * @date 2021-05-08
       */
      async addData() {
        this.$refs['mainForm'].validate(async (valid) => {
          if (valid) {

              let data = await this.$http.post('/auth/resource/save', this.formData)
              console.log(data);
              if (data.code === 200) {
                this.newDialog = false;
                this.initDatabase();
              } else {
                this.$message({
                  type: 'info',
                  message: '操作失败，请重试'
                });
              }
            }
        })

      }
    }
  }
</script>
